package com.azure.bigstore.controller;

import com.azure.bigstore.entity.Cart;
import com.azure.bigstore.entity.Token;
import com.azure.bigstore.service.CartService;
import com.azure.bigstore.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Created by liuhuaibin on 2018/1/26.
 */
@RestController
@RequestMapping("/v1")
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private TokenService tokenService;
    /**
     * get all cart item
     * @return
     */
    @GetMapping("/cart")
    public List<Cart> getCarts(){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String t = request.getHeader("t");
        long uid = 0L;
        List<Cart> cart = null;
        if("".equals(t)){
            response.setStatus(444);
        }else{
            Token token = tokenService.findByToken(t);
            //未查询到token
            if(token == null ){
                response.setStatus(444);
            }else{
                if(token.getValidityTime().after(new Date())){
                    uid = token.getUid();

                    long time = new Date().getTime()+1800000;//延长30分钟
                    System.out.println("now:"+new Date()+"  token time"+new Date(time));
                    token.setValidityTime(new Date(time));
                    tokenService.saveToken(token);

                    cart = cartService.getCartsByUid(uid);
                }else{
                    response.setStatus(444);
                }
            }
        }
        return cart;
    }
    /**
     * get an cart item
     * @param id cart_item_id
     * @return
     */
    @GetMapping("/cart/{id}")
    public Cart getCart(@PathVariable String id){
        return cartService.getCart(id);
    }

    /**
     * add cart item
     * @return cart
     */
    /*@PostMapping("/cart")
    public Cart addCart(Cart cart){
        return cartService.saveCart(cart);
    }
    */
    @PostMapping("/cart")
    public Cart addCart(){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String t = request.getHeader("t");//token
        String p = request.getHeader("p");//product id
        Cart cart = null;
        if(t != null && t != ""){
            Token token = tokenService.findByToken(t);
            //未查询到token
            if(token == null ){
                response.setStatus(444);
            }else{
                if(token.getValidityTime().after(new Date())){
                    //延长30分钟后的时间值
                    long time = new Date().getTime()+1800000;
                    System.out.println("new:"+new Date()+"  time"+new Date(time));
                    token.setValidityTime(new Date(time));
                    tokenService.saveToken(token);
                    long uid = token.getUid();
                    cart = new Cart(Long.valueOf(p),1,uid);
                }else{
                    response.setStatus(444);
                }
            }
        }
        return cartService.saveCart(cart);
    }

    /**
     * delete cart item
     * @return void
     */
    @DeleteMapping("/cart/{id}")
    public void addCart(@PathVariable String id){
        cartService.deleteCart(id);
    }
}
